package com.guigu.erp.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.erp.pojo.StorageCell;
import com.guigu.erp.pojo.StorageGather;
import com.guigu.erp.pojo.StoragePay;
import com.guigu.erp.pojo.StoragePayDetails;
import com.guigu.erp.service.StorageCellService;
import com.guigu.erp.service.StoragePayDetailsService;
import com.guigu.erp.service.StoragePayService;
import com.guigu.erp.utils.PageUtils;
import com.guigu.erp.vo.StoragePayVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;

/**
 * ClassName:StoragePayController
 * Package:com.guigu.erp.controller
 * Description:
 *
 * @Date:2024/3/26 21:12
 * @Author:chenchong
 */
@Controller
@RequestMapping("/StoragePay")
public class StoragePayController {
    @Autowired
    private StoragePayService storagePayService;
    @Autowired
    private StoragePayDetailsService storagePayDetailsService;

    @Autowired
    private StorageCellService storageCellService;

    @RequestMapping("/show")
    @ResponseBody
    private PageUtils<StoragePay> show(StoragePayVo vo, int pageNo, int pageSize) {
        QueryWrapper<StoragePay> qw = new QueryWrapper<>();
        if (vo != null) {
            if (!StringUtils.isEmpty(vo.getReason())) {
                qw.eq("REASON", vo.getReason());
            }
            if (!StringUtils.isEmpty(vo.getSearch_registerTime1())) {
                qw.ge("register_time", vo.getSearch_registerTime1());
            }
            if (!StringUtils.isEmpty(vo.getSearch_registerTime2())) {
                qw.le("register_time", vo.getSearch_registerTime2());
            }
        }
        qw.eq("STORE_TAG", "K002-1");

        PageHelper.startPage(pageNo, pageSize);
        List<StoragePay> list = storagePayService.list(qw);
        PageInfo<StoragePay> p = new PageInfo<>(list);
        PageUtils<StoragePay> utils = new PageUtils<>();
        utils.setTotal(p.getTotal());
        utils.setRows(p.getList());
        return utils;
    }

    @RequestMapping("/showById")
    @ResponseBody
    private StoragePay showById(int id) {
        QueryWrapper<StoragePay> qw = new QueryWrapper<>();
        qw.eq("id", id);
        StoragePay byId = storagePayService.getOne(qw);
        return byId;
    }

    @RequestMapping("/showById2")
    @ResponseBody
    private StoragePayDetails showById2(int id) {
        StoragePayDetails byId = storagePayDetailsService.getById(id);
        return byId;
    }

    @RequestMapping("/StoragePayDetailsShowById")
    @ResponseBody
    private PageUtils<StoragePayDetails> StoragePayDetailsShowById(int id) {
        QueryWrapper<StoragePayDetails> qw = new QueryWrapper<>();
        qw.eq("PARENT_ID", id);
        List<StoragePayDetails> list = storagePayDetailsService.list(qw);
        PageInfo<StoragePayDetails> p = new PageInfo<>(list);
        PageUtils<StoragePayDetails> utils = new PageUtils<>();
        utils.setTotal(p.getTotal());
        utils.setRows(p.getList());
        return utils;
    }

    @ResponseBody
    @RequestMapping("/updateTWO2")
    public boolean updateTWO(int id, int id1, int gatheredAmount) {
        StoragePayDetails byId = storagePayDetailsService.getById(id1);
        byId.setPayTag("K002-2");
        byId.setPaidAmount(gatheredAmount);
        storagePayDetailsService.updateById(byId);
        String productId = byId.getProductId();

        QueryWrapper<StoragePay> qw4 = new QueryWrapper<>();
        qw4.eq("id", id);
        StoragePay one1 = storagePayService.getOne(qw4);
        one1.setPaidAmountSum(one1.getPaidAmountSum()+gatheredAmount);

        storagePayService.updateById(one1);


        QueryWrapper<StorageCell> qw = new QueryWrapper<>();
        qw.eq("PRODUCT_ID", productId);
        StorageCell one = storageCellService.getOne(qw);
        BigDecimal b = new BigDecimal(gatheredAmount);
        one.setAmount(one.getAmount().subtract(b));
        storageCellService.updateById(one);

        QueryWrapper<StoragePayDetails> qw2 = new QueryWrapper<>();
        qw2.eq("PARENT_ID", id);
        qw2.eq("PAY_TAG", "K002-1");
        List<StoragePayDetails> list = storagePayDetailsService.list(qw2);
        boolean b1=false;
        StoragePay byId1 = storagePayService.getById(id);
        if (list.size() == 0) {
            byId1.setStoreTag("K002-2");
             b1 = storagePayService.updateById(byId1);
        }
        return b1;
    }
}
